<template>
  <base-breadcrumb :breadcrumb="!isAudit">
    <div class="about-master-container">
      <BForms ref="form">
        <a-card v-loading="loading">
          <title-name title="资产基本信息" />
          <BForm
            :config="formConfig"
            :form="form"
          />
        </a-card>
        <a-card
          v-if="form.assetAccessoriesInformations && form.assetAccessoriesInformations.length"
          v-loading="loading"
        >
          <title-name title="配件信息" />
          <part
            :disabled="true"
            :list.sync="form.assetAccessoriesInformations"
          />
        </a-card>
        <a-card v-loading="loading">
          <title-name title="调出单位信息" />
          <BForm
            :config="form2Config"
            :form="form"
          />
        </a-card>
        <a-card v-loading="loading">
          <title-name title="调入单位信息" />
          <BForm
            :config="form3Config"
            :form="form"
          />
        </a-card>
      </BForms>
      <div class="tar page-btn-right-top">
        <template v-if="!disabled">
          <a-button
            type="primary"
            :loading="submitLoading"
            :disabled="saveLoading"
            class="mr10p"
            @click="submit()"
          >
            提交
          </a-button>
          <a-button
            type="primary"
            :loading="saveLoading"
            :disabled="submitLoading"
            class="mr10p"
            @click="save('DRAFT',false)"
          >
            保存草稿
          </a-button>
        </template>
        <a-button
          v-if="!isConsultation"
          v-checkVisibleInDetail
          @click="$close"
        >
          关闭
        </a-button>
      </div>
    </div>
    <SelectPerson
      :visible.sync="modal.visible"
      mode="single"
      :org-id="modal.orgId"
      :list.sync="modal.list"
      @getSelectPerson="setSelectPerson($event)"
    />
    <assetList
      :visible.sync="modal.assetListVisible"
      @ok="getAssetListDetail"
    />
   <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
  </base-breadcrumb>
</template>

<script>
import assetApi from '@/api/office/asset'
import SelectCompanyAndProject from '@/components/Select/SelectCompanyAndProject'
import BUploadVModel from '@/components/BComponents/BUploadVModel'
import SelectOrgDepartment from '@/components/Select/SelectOrgDepartment'
import SelectOrgPeople from '@/components/Select/SelectOrgPeople'
import SelectPerson from '@/components/Select/SelectPerson'
import BSelect from '@/components/BComponents/BSelect'
import part from '@/views/office/assetManagement/check/part'
import assetList from './assetList'

export default {
  name:'AssetManagementTransferAdd',
  components: {
        SelectPerson,
        part,
        assetList
    },
    props: {
        isAudit: {
            type: Boolean,
            default: false
        },
      // 是否作为意见征询
        isConsultation: {
            type: Boolean,
            default: false
        }
    },
    watch: {
        isAudit: { // 是否作为审批详情
            immediate: true,
            handler(val) {
                if (val) {
                    this.disabled = true
                }
            }
        },
    },
    data() {
        return {
            submitLoading: false,
            saveLoading: false,
            modal: {
                visible: false,
                assetListVisible: false,
                list: [],
                orgId: '',
                type: -1
            },
            disabled: this.$route.query.detail == 1,
            form: {
                // assetTransferIn:{},
                'acceptanceTime': '',
                'accessories': null,
                'assetName': '',
                'assetSpecies': '',
                'assetType': null,
                'brand': '',
                'buildingArea': null,
                'carPurchase': null,
                'chassisNumber': null,
                'completionTime': null,
                'depId': '',
                'depName': '',
                'engineNumber': null,
                'fuelType': null,
                'houseAddress': null,
                'housePurpose': null,
                'leaveFactoryTime': '',
                'license': null,
                'manufactureUnit': '',
                'number': '',
                'productionUnit': null,
                'purchaseId': '',
                'purchaseName': '',
                'specModel': '',
                'status': '',
                'unit': '',
                'unitId': '',
                'unitName': '',
                'useAddress': '',
                'useDep': '',
                'useTime': null,
                'assetCode': null,
                'assetAccessoriesInformations': [],
                'assetDepreciationInformation': {
                    'baseId': '',
                    'durableYears': '',
                    'id': '',
                    'incidentals': '',
                    'originalValue': '',
                    'salvage': '',
                    'time': null,
                    'discountMonth': '',
                    'discountMoney': '',
                    'netValue': '',
                    'salvageValue': '',
                    'total': ''
                },
                'assetResponsibleInformation': {
                    'acceptorId': '',
                    'acceptorName': '',
                    'baseId': '',
                    'headId': '',
                    'headName': '',
                    'keepDepId': '',
                    'keepDepName': '',
                    'keepUnitId': '',
                    'keepUnitName': '',
                    'keeptorId': '',
                    'keeptorName': ''
                },
                'assetEnquiryFiles': [],
                'assetInvoiceFiles': [],
                'assetOtherFiles': [],
                'assetPurchaseFiles': [],
                'assetSpeciesStr': '',
                'assetTypeStr': null,
                'housePurposeStr': null,
                'statusStr': '',
                'assetDisposal': null,
                'assetDisposalApplyFiles': null,
                'assetDisposalPhotoFiles': null,
                'assetAuthenticateFiles': null,
                'assetDisposalFiles': null,
                'disposalId': null,
                'disposalName': null,
                'disposalTime': null,
                'applicantDepId': '',
                'applicantDepName': '',
                'applicantId': this.$userInfo().id,
                'applicantName': this.$userInfo().name,
                'assetTransferIn': {
                    'transferInAgentId': '',
                    'transferInAgentName': '',
                    'transferInDepId': undefined,
                    'transferInDepName': '',
                    'transferInFinancialId': '',
                    'transferInFinancialName': '',
                    'transferInHeadId': '',
                    'transferInHeadName': '',
                    'transferKeepDepId': undefined,
                    'transferKeepDepName': '',
                    'transferKeepId': undefined,
                    'transferKeepName': ''
                },
                'assetTransferOut': {
                    'transferOutAgentId': '',
                    'transferOutAgentName': '',
                    'transferOutDepId': '',
                    'transferOutDepName': '',
                    'transferOutFinancialId': '',
                    'transferOutFinancialName': '',
                    'transferOutHeadId': '',
                    'transferOutHeadName': '',
                    'transferOutShipperId': '',
                    'transferOutShipperName': ''
                },
                'createTime': '',
                'creatorId': '',
                'isDeleted': '',
                'keepDepId': '',
                'keepDepName': '',
                'keepUnitId': '',
                'keepUnitName': '',
                'keeptorId': '',
                'keeptorName': '',
                'modifiedTime': '',
                'originalValue': '',
                'parameterId': ''
            },
            //获取流程配置值
            processConfig: null,
            //点击提交发送流程配置值
            setProcessConfig: null,
            businessId: null
        }
    },
    computed: {
      /* eslint-disable */
        formConfig() {
            return [
                {
                    row: true,
                    children: [
                        {
                            type: SelectCompanyAndProject, value: 'applicantDepId', label: '申请人单位',
                            attrs: { disabled: true, autoSelect: !this.$route.query.id, isAuth: !this.disabled },
                            on: {
                                onSelectChange: (e) => {
                                    this.form.applicantDepName = e.name
                                }
                            }
                        },
                        { type: 'input', value: 'applicantName', label: '申请人', attrs: { disabled: true } },
                        {
                            type: 'input-search', value: 'assetCode', label: '资产编号',
                            rules: { required: true, message: '资产编号不能为空', trigger: 'blur' },
                            attrs: { disabled: this.disabled },
                            on: {
                                click: () => this.modal.assetListVisible = true,
                                search: () => this.modal.assetListVisible = true
                            }
                        },
                        {
                            type: BSelect, value: 'assetSpecies', label: '资产类别',
                            attrs: {
                                disabled: true,
                                request: () => assetApi.typesLabel({ type: 18 })
                            },
                            on: {
                                change: () => {
                                    if (this.form.assetSpecies === '605004528177254572') {
                                        this.form.assetType = ''
                                    }
                                }
                            }
                        },
                        { type: 'input', value: 'assetName', label: '资产名称', attrs: { disabled: true } },
                        { type: 'input', value: 'unit', label: '单位', attrs: { disabled: true } },
                        {
                            type: 'number', value: 'originalValue', label: '原值(元)',
                            attrs: { disabled: true, precision: 2 }
                        },
                        {
                            type: 'input', value: 'keepUnitName', label: '保管单位',
                            attrs: { disabled: true }
                        },
                        {
                            type: 'input', value: 'keepDepName', label: '保管部门',
                            attrs: { disabled: true }
                        },
                        {
                            type: 'input', value: 'keeptorName', label: '保管人',
                            attrs: { disabled: true }
                        }
                    ]
                }
            ]
        },
        form2Config() {
            return [
                {
                    row: true,
                    children: [
                        {
                            type: 'input', value: 'assetTransferOut.transferOutDepName', label: '调出单位',
                            attrs: { disabled: true }
                        },
                        {
                            type: 'input-search', value: 'assetTransferOut.transferOutHeadName', label: '负责人',
                            attrs: { disabled: true }
                        },
                        {
                            type: 'input-search', value: 'assetTransferOut.transferOutFinancialName', label: '财务',
                            rules: { required: true, message: '财务不能为空', trigger: 'change' },
                            attrs: { disabled: this.disabled },
                            on: {
                                click: () => this.selectPerson(this.form.keepUnitId, 1),
                                search: () => this.selectPerson(this.form.keepUnitId, 1)
                            }
                        },
                        {
                            type: 'input-search', value: 'assetTransferOut.transferOutAgentName', label: '经办人',
                            rules: { required: true, message: '经办人不能为空', trigger: 'change' },
                            attrs: { disabled: this.disabled },
                            on: {
                                click: () => this.selectPerson(this.form.keepUnitId, 2),
                                search: () => this.selectPerson(this.form.keepUnitId, 2)
                            }
                        },
                        {
                            type: 'input-search', value: 'assetTransferOut.transferOutShipperName', label: '发货人',
                            attrs: { disabled: this.disabled },
                            rules: { required: true, message: '发货人不能为空', trigger: 'change' },
                            on: {
                                click: () => this.selectPerson(this.form.keepUnitId, 3),
                                search: () => this.selectPerson(this.form.keepUnitId, 3)
                            }
                        }
                    ]
                }
            ]
        },
        form3Config() {
            return [
                {
                    row: true,
                    children: [
                        {
                            type: SelectCompanyAndProject, value: 'assetTransferIn.transferInDepId', label: '调入单位',
                            show: !this.disabled,
                            rules: { required: true, message: '调入单位不能为空', trigger: 'change' },
                            attrs: { disabled: this.disabled, autoSelect: false, isAuth: !this.disabled },
                            on: {
                                onSelectChange: (e) => {
                                    this.form.assetTransferIn.transferInDepName = e.name
                                    this.form.assetTransferIn.transferKeepDepId = void 0
                                    this.form.assetTransferIn.transferKeepDepName = void 0
                                    this.form.assetTransferIn.transferKeepId = void 0
                                    this.form.assetTransferIn.transferKeepName = void 0

                                    this.form.assetTransferIn.transferInHeadId = void 0
                                    this.form.assetTransferIn.transferInHeadName = void 0
                                    this.form.assetTransferIn.transferInFinancialId = void 0
                                    this.form.assetTransferIn.transferInFinancialName = void 0
                                    this.form.assetTransferIn.transferInAgentId = void 0
                                    this.form.assetTransferIn.transferInAgentName = void 0
                                }
                            }
                        },
                        {
                            type: 'input', value: 'assetTransferIn.transferInDepName', label: '调入单位',
                            show: this.disabled,
                            attrs: { disabled: true }
                        },
                        {
                            type: 'input-search', value: 'assetTransferIn.transferInHeadName', label: '负责人',
                            attrs: { disabled: this.disabled },
                            rules: { required: true, message: '负责人不能为空', trigger: 'change' },
                            on: {
                                click: () => this.selectPerson(this.form.assetTransferIn.transferInDepId, 4),
                                search: () => this.selectPerson(this.form.assetTransferIn.transferInDepId, 4)
                            }
                        },
                        {
                            type: 'input-search', value: 'assetTransferIn.transferInFinancialName', label: '财务',
                            rules: { required: true, message: '财务不能为空', trigger: 'change' },
                            attrs: { disabled: this.disabled },
                            on: {
                                click: () => this.selectPerson(this.form.assetTransferIn.transferInDepId, 5),
                                search: () => this.selectPerson(this.form.assetTransferIn.transferInDepId, 5)
                            }
                        },
                        {
                            type: 'input-search', value: 'assetTransferIn.transferInAgentName', label: '经办人',
                            rules: { required: true, message: '经办人不能为空', trigger: 'change' },
                            attrs: { disabled: this.disabled },
                            on: {
                                click: () => this.selectPerson(this.form.assetTransferIn.transferInDepId, 6),
                                search: () => this.selectPerson(this.form.assetTransferIn.transferInDepId, 6)
                            }
                        },
                        {
                            type: SelectOrgDepartment, value: 'assetTransferIn.transferKeepDepId', label: '保管部门',
                            rules: { required: true, message: '保管部门不能为空', trigger: 'change' },
                            show: !this.disabled,
                            attrs: {
                                orgId: this.form.assetTransferIn.transferInDepId,
                                // disabled: this.disabled,
                                autoSelected: false
                            },
                            on: {
                                onSelectChange: (e) => {
                                    this.form.assetTransferIn.transferKeepDepName = e.name
                                    this.form.assetTransferIn.transferKeepId = void 0
                                    this.form.assetTransferIn.transferKeepName = void 0
                                }
                            }
                        },
                        {
                            type: 'input', value: 'assetTransferIn.transferKeepDepName', label: '保管部门',
                            show: this.disabled,
                            attrs: { disabled: true }
                        },
                        {
                            type: SelectOrgPeople, value: 'assetTransferIn.transferKeepId', label: '保管人',
                            rules: { required: true, message: '保管人不能为空', trigger: 'change' },
                            attrs: {
                                orgId: this.form.assetTransferIn.transferKeepDepId,
                                disabled: this.disabled,
                                isOrgIdNotNull: true
                            },
                            on: {
                                onSelectChange: (e) => this.form.assetTransferIn.transferKeepName = e.name
                            }
                        }
                    ]
                }
            ]
        },
        loading() {
            return this.submitLoading || this.saveLoading
        }
    },
    created: async function() {
        if (this.$route.query.id) {
            this.form.id = this.$route.query.id
            await this.getBaseInfo()
        }
     //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('asset_transfers_audit')  
    },
    methods: {
        async getAssetListDetail(e) {
            this.saveLoading = true
            let res = await assetApi.findByIdParameter({}, { id: e.id })
            this.saveLoading = false
            if (res.code === this.SUCCESS_CODE) {
                this.form.parameterId = res.data.id
                delete res.data.id
                this.form = Object.assign(this.form, res.data)
                this.form.assetTransferOut.transferOutDepId = this.form.assetResponsibleInformation.keepUnitId
                this.form.assetTransferOut.transferOutDepName = this.form.assetResponsibleInformation.keepUnitName

                this.form.assetTransferOut.transferOutHeadName = this.form.assetResponsibleInformation.headName
                this.form.assetTransferOut.transferOutHeadId = this.form.assetResponsibleInformation.headId

                this.form.originalValue = this.form.assetDepreciationInformation.originalValue
                this.form.keepUnitId = this.form.assetResponsibleInformation.keepUnitId
                this.form.keepUnitName = this.form.assetResponsibleInformation.keepUnitName
                this.form.keepDepId = this.form.assetResponsibleInformation.keepDepId
                this.form.keepDepName = this.form.assetResponsibleInformation.keepDepName
                this.form.keeptorId = this.form.assetResponsibleInformation.keeptorId
                this.form.keeptorName = this.form.assetResponsibleInformation.keeptorName
            }
        },
        selectPerson(orgId, type) {
            if (!orgId) {
                if (type === 1 || type === 2 || type === 3) return this.$noticeWarning('请选择资产编号')
                return this.$noticeWarning('请选择调入单位信息')
            }
            this.modal.orgId = orgId
            this.modal.type = type
            this.modal.visible = true
            this.modal.list = []
            switch (type) {
                case 1:
                    if (this.form.assetTransferOut.transferOutFinancialId) {
                        this.modal.list = [{
                            id: this.form.assetTransferOut.transferOutFinancialId,
                            name: this.form.assetTransferOut.transferOutFinancialName
                        }]
                    }
                    break
                case 2:
                    if (this.form.assetTransferOut.transferOutAgentId) {
                        this.modal.list = [{
                            id: this.form.assetTransferOut.transferOutAgentId,
                            name: this.form.assetTransferOut.transferOutAgentName
                        }]
                    }
                    break
                case 3:
                    if (this.form.assetTransferOut.transferOutShipperId) {
                        this.modal.list = [{
                            id: this.form.assetTransferOut.transferOutShipperId,
                            name: this.form.assetTransferOut.transferOutShipperName
                        }]
                    }
                    break
                case 4:
                    if (this.form.assetTransferIn.transferInHeadId) {
                        this.modal.list = [{
                            id: this.form.assetTransferIn.transferInHeadId,
                            name: this.form.assetTransferIn.transferInHeadName
                        }]
                    }
                    break
                case 5:
                    if (this.form.assetTransferIn.transferInFinancialId) {
                        this.modal.list = [{
                            id: this.form.assetTransferIn.transferInFinancialId,
                            name: this.form.assetTransferIn.transferInFinancialName
                        }]
                    }
                    break
                case 6:
                    if (this.form.assetTransferIn.transferInAgentId) {
                        this.modal.list = [{
                            id: this.form.assetTransferIn.transferInAgentId,
                            name: this.form.assetTransferIn.transferInAgentName
                        }]
                    }
                    break
            }
        },
        setSelectPerson(e) {
            if (!e) return
            switch (this.modal.type) {
                case 1:
                    this.form.assetTransferOut.transferOutFinancialId = e[0].userId
                    this.form.assetTransferOut.transferOutFinancialName = e[0].name
                    break
                case 2:
                    this.form.assetTransferOut.transferOutAgentId = e[0].userId
                    this.form.assetTransferOut.transferOutAgentName = e[0].name
                    break
                case 3:
                    this.form.assetTransferOut.transferOutShipperId = e[0].userId
                    this.form.assetTransferOut.transferOutShipperName = e[0].name
                    break
                case 4:
                    this.form.assetTransferIn.transferInHeadId = e[0].userId
                    this.form.assetTransferIn.transferInHeadName = e[0].name
                    break
                case 5:
                    this.form.assetTransferIn.transferInFinancialId = e[0].userId
                    this.form.assetTransferIn.transferInFinancialName = e[0].name
                    break
                case 6:
                    this.form.assetTransferIn.transferInAgentId = e[0].userId
                    this.form.assetTransferIn.transferInAgentName = e[0].name
                    break
            }
        },
        async getBaseInfo() {
            this.saveLoading = true
            let res = await assetApi.assetTransfersDetail({}, { id: this.form.id })
            if (res.code === this.SUCCESS_CODE) {
                this.form = res.data
                this.form.assetTransferIn.transferInDepId = this.form.assetTransferIn.transferInDepId || undefined
                this.form.assetTransferIn.transferKeepDepId = this.form.assetTransferIn.transferKeepDepId || undefined
                this.form.assetTransferIn.transferKeepId = this.form.assetTransferIn.transferKeepId || undefined
            }
            this.saveLoading = false
        },
        async submit() {
            if (!await this.save('DRAFT', true)) {
                return
            }
            if (await this.save('SUBMITTED', true)) {
            }
        },
        async save(auditStatus, isSubmit = false) {
            this.setProcessConfig = null; 
            return new Promise(async resolve => {
                let isPass = true
                if (isSubmit) {
                    this.$refs.form.validate(valid => {
                        isPass = valid
                    })
                } else {
                    isPass = true
                }
                if (isPass) {
                    let res
                    let temp = this.$clone(this.form)
                    temp.auditStatus = auditStatus

                    if (!isSubmit) {
                        this.saveLoading = true
                    } else {
                        this.submitLoading = true
                    }
                    if (temp.id) {
                        res = await assetApi.assetTransfersUpdate(temp)
                    } else {
                        res = await assetApi.assetTransfersAdd(temp)
                    }
                    if (res.code === 200) {
                        this.form.id = res.data.id
                      await this.getBaseInfo()
                      if (auditStatus === 'DRAFT') {
                            if (!isSubmit) {
                                this.$message.success('保存成功')
                            }
                        } else {
                            this.$message.success('提交成功');
                           //提交时，返回id
                           this.businessId = res.data.id;
                           this.setProcessConfig = this.processConfig;
                        }
                        resolve(true)
                    }
                    this.saveLoading = false
                    this.submitLoading = false
                }
            })
        }
    }
}
</script>
